﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using New_WP8_Mapping_App.Resources;
using Windows.Devices.Geolocation;
using System.Device.Location;

namespace New_WP8_Mapping_App
{
    public partial class MainPage : PhoneApplicationPage
    {
        Geolocator locator;  
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            locator = new Geolocator();

            this.Loaded += MainPage_Loaded;
        }

        async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                locator.DesiredAccuracy = PositionAccuracy.High;
                locator.MovementThreshold = 50;
                locator.PositionChanged += locator_PositionChanged;
                Geoposition myLocation = await locator.GetGeopositionAsync();
                GeoCoordinate geoCord = new GeoCoordinate(myLocation.Coordinate.Latitude, myLocation.Coordinate.Longitude);
                MyFirstMap.Center = geoCord;
                MyFirstMap.ZoomLevel = 16;
            }
            catch (Exception exception)
            {
            }            
        }

        void locator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
        {
            GeoCoordinate newGeoCoord = new GeoCoordinate(
                    args.Position.Coordinate.Latitude,
                    args.Position.Coordinate.Longitude);

            Dispatcher.BeginInvoke(() =>
            {
                MyFirstMap.Center = newGeoCoord;
            });
        }

    }
}